/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.demo.controller;

import com.demo.exception.RestException;
import com.demo.model.Account;
import com.demo.service.AccountService;
import com.demo.service.RoleService;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author student
 */
@Controller
public class AccountController {
    
        @Autowired
        AccountService accountService;
        
        @Autowired
        RoleService roleService;
		
		@RequestMapping(value = "account_list.htm", method = RequestMethod.GET)
	public ModelAndView getRestList(
                HttpServletResponse httpServletResponse) {
            ModelAndView mav = new ModelAndView("jsonView");
			
            try {
                List list = accountService.list();
                List roles = roleService.getAll();
                mav.addObject("roles",roles);        
                mav.addObject("list",list);
                mav.addObject("res", true);

            } catch (RestException ex) {
                mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            }
            return mav;
	}
	                    
        @RequestMapping(value = "account_item.htm", method = RequestMethod.GET)
	public ModelAndView getRest(
                HttpServletResponse httpServletResponse,
		@RequestParam(value = "id", required = true) long id) {
            ModelAndView mav = new ModelAndView("jsonView");
            
            try {
                Account item = accountService.get(id);
                mav.addObject("item", item);
                mav.addObject("res", true);
                
            } catch (RestException ex) {
                mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            }
            return mav;
        }
        
        @RequestMapping(value = "account_edit.htm", method = RequestMethod.POST)
	public ModelAndView putRest(
                HttpServletResponse httpServletResponse,
		@RequestParam(value = "id", required = true) long id,
                @RequestParam(value = "email", required = true) String email,
                @RequestParam(value = "fname", required = true) String fname,
                @RequestParam(value = "lname", required = true) String lname,
                @RequestParam(value = "zip", required = true) String zip,
                @RequestParam(value = "gender", required = true) String gender,
                @RequestParam(value = "role", required = true) long role) {
            ModelAndView mav = new ModelAndView("jsonView");
            
            try {
                Account item = accountService.put(id, email, fname, lname, zip, gender, role);
                mav.addObject("item", item);
                mav.addObject("res", true);
            
            } catch (RestException ex) {
                mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            }
            return mav;
	}
        
        @RequestMapping(value = "account_add.htm", method = RequestMethod.POST)
	public ModelAndView postRest(
                HttpServletResponse httpServletResponse,
                @RequestParam(value = "email", required = true) String email,
                @RequestParam(value = "fname", required = true) String fname,
                @RequestParam(value = "lname", required = true) String lname,
                @RequestParam(value = "zip", required = true) String zip,
                @RequestParam(value = "gender", required = true) String gender,
                @RequestParam(value = "role", required = true) long role) {
            ModelAndView mav = new ModelAndView("jsonView");
            
            try {
                Account item = accountService.post(email, fname, lname, zip, gender, role);
                mav.addObject("item", item);
                mav.addObject("res", true);
                
            } catch (RestException ex) {
                mav.addObject("error", ex.getMessage());
                mav.addObject("res", false);
            }
            return mav;
	}
		
		@RequestMapping(value = "account_delete.htm", method = RequestMethod.GET)
	public ModelAndView deleteRest(
                HttpServletResponse httpServletResponse,
		@RequestParam(value = "id", required = true) long id) {
            ModelAndView mav = new ModelAndView("jsonView");            
            
            try {
                boolean item = accountService.delete(id);
                mav.addObject("res", true);
                
            } catch (RestException ex) {
                mav.addObject("item", ex.getMessage());
                mav.addObject("res", false);
            }
            return mav;
	}
        
}
